import Vue from 'vue'
import VueRouter from 'vue-router'

// 引入对应的页面
// 账号管理
import AccountAdd from "@/views/account/AccountAdd.vue"
import AccoutList from "@/views/account/AccoutList.vue"
import ModifyPassword from "@/views/account/ModifyPassword.vue"
import PersonalCenter from "@/views/account/PersonalCenter.vue"

// 商品管理
import GoodsAdd from "@/views/goods/GoodsAdd.vue"
import GoodsCategroy from "@/views/goods/GoodsCategroy.vue"
import GoodsList from "@/views/goods/GoodsList.vue"

import { Message } from 'element-ui';

// 后台首页
import Home from "@/views/home/Home.vue"

// 后台大页面，分为左右两部分
import Layout from "@/views/layout/Layout.vue"

// 登录页面
import Login from "@/views/login/Login.vue"

// 订单管理
import OrderManage from "@/views/order/OrderManage.vue"
import OrderMore from "@/views/order/OrderMore.vue"

// 销售统计
import GoodsTotal from "@/views/sell/GoodsTotal.vue"
import OrderTotal from "@/views/sell/OrderTotal.vue"

// 店铺管理
import ShopManage from "@/views/shop/ShopManage.vue"

Vue.use(VueRouter)

// const routes = [
//   // 配置一级路由
//   { path: "/", redirect: "/Layout" },

//   // 后台大页面
//   { path: "/layout", component: Layout },

// ]

// 将路由权限表拆分
// 一个是所有人都能访问的数组表
// 一个根据角色所访问的数组表
let common = [
  // 登录路由
  { path: "/login", component: Login },
  // 后台首页
  {
    path: "/Home",
    component: Layout,
    redirect: "/Home/Home",
    meta: { title: "后台首页", type: 1 },
    children: [
      { path: "/Home/Home", component: Home }
    ]
  },
]
let asyncList = [  // 下面是二级路由
  // 订单管理
  {
    path: "/Order",
    component: Layout,
    redirect: "/Order/OrderManage",
    meta: { title: "订单管理", permission: ["super", "normal"], type: 1 },
    children: [
      { path: "/Order/OrderManage", meta: { permission: ["super", "normal"] }, component: OrderManage },
      { path: "/Order/OrderMore", meta: { permission: ["super", "normal"] }, component: OrderMore },
    ]
  },
  // 商品管理
  {
    path: "/Goods",
    component: Layout,
    redirect: "/Goods/GoodsAdd",
    meta: { title: "商品管理", permission: ["super", "normal"], type: 2 },
    children: [
      { path: "/Goods/GoodsAdd", meta: { title: "商品添加", permission: ["super", "normal"] }, component: GoodsAdd },
      { path: "/Goods/GoodsCategroy", meta: { title: "商品分类", permission: ["super", "normal"] }, component: GoodsCategroy },
      { path: "/Goods/GoodsList", meta: { title: "商品列表", permission: ["super", "normal"] }, component: GoodsList }
    ]
  },


  // 店铺管理
  {
    path: "/Shop",
    component: Layout,
    redirect: "/Shop/ShopManage",
    meta: { title: "店铺管理", permission: ["super"], type: 1 },
    children: [
      { path: "/Shop/ShopManage", component: ShopManage }
    ]
  },

  // 账号管理
  {
    path: "/Account",
    component: Layout,
    redirect: "/Account/AccoutList",
    meta: { title: "账号管理", permission: ["super", "normal"], type: 2 },
    children: [
      { path: "/Account/AccoutList", meta: { title: "账号列表", permission: ["super"] }, component: AccoutList },
      { path: "/Account/AccountAdd", meta: { title: "账号添加", permission: ["super"] }, component: AccountAdd },
      { path: "/Account/ModifyPassword", meta: { title: "修改密码", permission: ["super"] }, component: ModifyPassword },
      { path: "/Account/PersonalCenter", meta: { title: "个人中心", permission: ["super", "normal"] }, component: PersonalCenter },
    ]
  },

  // 销售统计
  {
    path: "/Total",
    component: Layout,
    redirect: "/Total/GoodsTotal",
    meta: { title: "销售统计", permission: ["super"], type: 2 },
    children: [
      { path: "/Total/GoodsTotal", meta: { title: "商品统计", permission: ["super"] }, component: GoodsTotal },
      { path: "/Total/OrderTotal", meta: { title: "订单统计", permission: ["super"] }, component: OrderTotal },
    ]
  },

]

const router = new VueRouter({
  routes: common
})


router.beforeEach((to, from, next) => {
  // 如果跳转的页面是登录页，那么就不要求它有token，让它直接跳转
  if (to.path == "/login") {
    next()
  } else {
    let token = localStorage.getItem("token")
    if (token) {
      next()
    } else {
      // this.$router.push("/login")
      Message({
        type: "error",
        message: "登录失效，请重新登录，2秒之后跳转到登录界面！"
      })
      setTimeout(() => {
        next("/login")
      }, 2000);
    }
  }
})


// 写一个函数计算当前账号需要的路由表
export function renderMenu() {
  let role = localStorage.getItem("role")
  // 如果还没登录,则不计算路由数组
  if (!role) {
    return
  }

  // 一级路由权限判断
  for (let x = 0; x < asyncList.length; x++) {
    if (asyncList[x].meta && asyncList[x].meta.permission && !asyncList[x].meta.permission.includes(role)) {
      asyncList.splice(x, 1)
      x--
    }
  }


  // 找到二级菜单
  for (let q = 0; q < asyncList.length; q++) {
    if (asyncList[q].children.length > 1) {
      for (let w = 0; w < asyncList[q].children.length; w++) {
        let item = asyncList[q].children[w]
        if (item.meta && item.meta.permission && !item.meta.permission.includes(role)) {
          asyncList[q].children.splice(w, 1)
          w--
        }
      }
    }
  }
  // 二级路由权限判断
  router.addRoutes(asyncList)
  localStorage.setItem("asyncList", JSON.stringify([{
    path: "/Home",
    component: Layout,
    redirect: "/Home/Home",
    meta: { title: "后台首页", type: 1 },
    children: [
      { path: "/Home/Home", component: Home }
    ]
  }, ...asyncList]))
}

renderMenu()

export default router
